import { Component, OnInit, ViewChild } from '@angular/core';
import { TreeEditComponent } from '../../../../components/tree-edit/tree-edit.component';
import { InspectPropertySettingService } from '../../../../services/project/inspection/inspect-property-setting.service'
import { ActivatedRoute, Router } from '@angular/router';
import { NzMessageService } from 'ng-zorro-antd';

@Component({
  selector: 'app-inspect-property-setting',
  templateUrl: './inspect-property-setting.component.html',
  styleUrls: ['./inspect-property-setting.component.scss']
})
export class InspectPropertySettingComponent implements OnInit {
  @ViewChild('treeEdit', { static: false }) public treeEdit: TreeEditComponent;

  constructor(
    private router: Router,
    private inspectPropertySettingService: InspectPropertySettingService,
    private routerInfo: ActivatedRoute,
    public msg: NzMessageService
  ) { }

  public nodesList: any = [{
    id: '',
    name: ''
  }]
  public projectId: string;
  public name: string = JSON.parse(window.localStorage.getItem('project')).name;
  public treeAttributes: any = {
    id: 'id',
    parentId: 'parentId',
    name: 'name',
    rootId: "00000000-0000-0000-0000-000000000000"
  };
  /**
   * 获取树结构
   */
  public async getCheckNode() {
    // const projectId = this.routerInfo.snapshot.params['projectId'];
    this.projectId = JSON.parse(window.localStorage.getItem('project')).id;
    const params = {
      projectId: this.projectId,
      type: 2
    }
    const res = await this.inspectPropertySettingService.QueryCheckItems(params)
    this.nodesList = this.treeEdit.transformTreeNode(res.result, this.treeAttributes);
    console.log(this.nodesList)
  }
  /**
   * 新增/编辑接口
   */
  public async handleCreateOrModify() {
    const params = {
      projectId: this.projectId,
      type: 2,
      name: this.treeEdit.addEditValue,
      // maxResultCount: 1000,
      // skipCount: 0
    }
    if (this.treeEdit.nodeStauts === 2) {
      // 编辑
      params['id'] = this.treeEdit.activedNode.key
    } else if (this.treeEdit.nodeStauts === 1) {
      // 新增
      params['parentId'] = this.treeEdit.activedNode ? this.treeEdit.activedNode.key : '00000000-0000-0000-0000-000000000000'
    }
    if (this.treeEdit.addEditValue.length == 0) {
      this.msg.warning('内容不能为空')
      return;
    } else{
       await this.inspectPropertySettingService.CreateOrModifyCheckItem(params)
    await this.getCheckNode()
    this.treeEdit.isAddEditModalVisible = false;
    }
  }
  /**
   * 刷新节点 
   */
  refreshNode(id?) {
    if (this.treeEdit.activedNode || this.treeEdit.activedNode.parentNode) {
      // 非根目录
      if (this.treeEdit.nodeStauts === 2) {
        // 2 编辑
        this.treeEdit.activedNode.title = this.treeEdit.addEditValue
      } else if (this.treeEdit.nodeStauts === 1) {
        // 1 新增
        let newNode = [{
          title: this.treeEdit.addEditValue,
          key: id
        }]
        this.treeEdit.activedNode['expanded'] = true
        this.treeEdit.activedNode['isExpanded'] = true
        this.treeEdit.activedNode.addChildren(newNode)
      } else {
        // 3 删除
        // this.treeEdit.activedNode.parentNode.clearChildren();
        this.getCheckNode()
      }
    } else {
      // 根目录
      this.getCheckNode()
    }
  }
  /**
   * 删除接口
   */
  public async handleDelete(deleteArray) {
    await this.inspectPropertySettingService.BatchRemoveCheckItem(deleteArray).then(res => {
      this.treeEdit.activedNode = null
      this.getCheckNode()
    })
  }
  /**
   * 新增模态框
   */
  public openAddModal() {
    this.treeEdit.resetForm()
    this.treeEdit.nodeStauts = 1;
    this.treeEdit.isAddEditModalVisible = true;
    let EditInput = document.getElementById('EditInput');
    setTimeout(() => {
      EditInput.focus();
    }, 0);
  }

  routeToSurvey() {
    const projectId = JSON.parse(window.localStorage.getItem('project')).id;
    this.router.navigate([`/inner/project/${projectId}/survey`], {queryParams: { info: 'project' }});
  }
  ngOnInit() {
    this.getCheckNode()
  }

}
